<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="../../util/taglib.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>角色数据列表</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">
	<jsp:include page="../util/commScript.jsp">
	   <jsp:param value="<%=basePath %>" name="basePath"/>
	</jsp:include>
  </head>
  <body>
  <div class="place">
    <span>位置：</span>
	    <ul class="placeul">
		    <li><a href="javascript:void(0)">首页</a></li>
		    <li><a href="javascript:void(0)">系统管理</a></li>
		    <li><a href="javascript:void(0)">角色管理</a></li>
	    </ul>
	</div>
    <div id="groupListPage"  class="easyui-layout"  data-options="fit:'true'">
		<input type="hidden" name="companyUUID" id="companyUUID" value="${param.companyUUID }" >
		<div class="groupTree" data-options="region:'west',split:true,title:'角色列表',tools:[
				{
					iconCls:'icon-add',
					handler:function(){
					  addData();
				    }
				},{
					iconCls:'icon-no',
					handler:function(){
					  delData();
					}
				}]" title="West" style="width:240px;padding:3px">
		</div>
		<div data-options="region:'center',noheader:true" title="Center">
		    <div id="groupListPage"  class="easyui-layout"  data-options="fit:'true'">
				<input type="hidden" name="companyUUID" id="companyUUID" value="${param.companyUUID }" >
				<div class="permissionTree" data-options="region:'west',split:true,title:'权限树',tools:[
				{
					iconCls:'icon-save',
					handler:function(){
					   savePms2Group();
				    }
				}]" title="West" style="width:240px;padding:3px">
				</div>
				<div class="havePms" data-options="region:'center',title:'已有权限'" title="Center">
				    
				</div>
			</div>
		</div>
      <script type="text/javascript">
          var tableObject = $("#groupListPage .groupList");
          var basePath = getBasePath();
          $(function(){
        	  //加载角色树
        	  loadGroupTree();
        	  //加载权限树
        	  loadPmsTree();
          })
          
          //保存权限结果
          function savePms2Group(){
        	  //获取当前选中的选项
        	  var groupTree = $('#groupListPage .groupTree');
        	  var node =  groupTree.tree("getSelected");
        	  if(null==node){
        		  $.messager.alert('系统提示',"请先选中要修改权限的角色选项！否则无效！",'info');
        		  return;
        	  }
        	  $.messager.confirm('确认对话框', '此操作将修改'+node.text+'权限内容,请确认是否继续操作？',function(flag){
        			if (flag){
        				var permissionArray = $("#groupListPage .havePms").text();
        				$.post(basePath+"/group/savePms2Group.do",{uuid:node.id,permissionArray:permissionArray},
        		    	function(jsonData){
        					if(jsonData.code=="00"){
        						$.messager.alert('系统提示',jsonData.msg,'success');
        						$('#groupListPage .groupTree').tree('reload');
        					}else{
        						$.messager.alert('系统提示',jsonData.msg,'error');
        					}
        		    	},"json");
        			}
        	 });
          }
          
          //加载角色树
          function loadGroupTree(){
        	  var groupTree = $('#groupListPage .groupTree');
        	  var url = getBasePath()+"group/getGroupList4Tree.do"
        	  groupTree.tree({    
          	        url:url,
          	        method: 'post',
	          		animate: true,
	          		lines:true,
	          		//双击某个节点的时候
	          		onDblClick: function(node){
	        		    $(this).tree('beginEdit',node.target);
	        		},
	        		//编辑完后的事件
	        		onAfterEdit: function(node){
	        			//修改当前节点
	        			$.post(getBasePath()+"group/updateGroupName.do",{
	        				uuid:node.id,
	        				groupName:node.text
	        			},function(data){
	        				//alert(data.actionFlag);
	        			},"json");
	        		},
	          		//选中某个节点时触发
	          		onSelect:function(node){
	          			//加载权限树
	          			$.messager.progress(); 
	          			loadPmsTree(node.id);
	          		}
	          });
          }
          
          //加载权限树
    	  function loadPmsTree(uuid){
        	  var pmsTree = $('#groupListPage .permissionTree');
        	  var url = getBasePath()+"permission/getPermissionTree.do"
        	  pmsTree.tree({
          	        url:url,
          	        method: 'post',
	          		animate: true,
	          		checkbox:true,
	          		lines:true,
	          		queryParams:{
	          			groupUUID:uuid
	          		},
	          		//单击某个节点触发的事件
	          		onCheck:function(node,checked){
	          			var groupTree = $('#groupListPage .groupTree');
	                	//得到选中的节点
	                	var selectNode = groupTree.tree("getSelected");
	                	var nodes = pmsTree.tree('getChecked', ['checked','indeterminate']);
	                	if(null==selectNode){
	                		$.messager.alert('系统提示',"请先选中要修改权限的角色选项！否则无效！",'info');
	                		return;
	                	}
	                	var pmsStr = "";
	          			$.each(nodes,function(i,node){
	          				pmsStr+="["+node.attributes.psmCode+"]:"+node.text+"<br/>";
	          			})
	          			$("#groupListPage .havePms").html(pmsStr);
	          		},
	          		onLoadSuccess:function(node,data){
	          			var groupTree = $('#groupListPage .groupTree');
	          		    //得到选中的节点
	                	var selectNode = groupTree.tree("getSelected");
	                	var nodes = pmsTree.tree('getChecked', ['checked','indeterminate']);
	                	var pmsStr = "";
	          			$.each(nodes,function(i,node){
	          				pmsStr+="["+node.attributes.psmCode+"]:"+node.text+"<br/>";
	          			})
	          			$("#groupListPage .havePms").html(pmsStr);
	          			$.messager.progress('close');
	          		}
	          });  
          }
        //添加数据
  	    function addData(id){
  	        //验证是否登录--开始
  	    	if(!checkLoginStatus()){return;}
  	        //验证是否登录--结束
  	        
  	    	var editDataFormArea = $("#editDataFormArea");
      		if(editDataFormArea.length==0){
      			$("<div id='editDataFormArea'></div>").prependTo("body");
      		}
      	    //得到左边树节点当前选中的项
      		var curNode = $('#groupTree').tree("getSelected");
      		var companyUUID = "";
      		if(curNode){companyUUID = curNode.id;}
      	    //计算距离
      		var _width = 350;
      		var _height = 220;
      		var x_left = ($(window).width()-_width)/2;
      		var y_top = ($(window).height()-_height)/2;
  	    	$("#editDataFormArea").window({
  	    	    title: '添加角色',
  	    	    width: _width,
  	    	    height: _height,
  	    	    iconCls:'icon-window',
  	    	    left:x_left,
  	    	    top:y_top,
  	    	    closed: false,
  	    	    cache: false,
  	    	    modal: true,
  	    	    onClose:function(){
				    $("#editDataFormArea").html("");
				    $("#editDataFormArea").remove();
				},
				href:basePath+"manage/xtsz/editGroup.jsp"
  	    	});
  	    }
          
        //删除单条数据	
  	    function delData(id){
        	//验证是否登录--开始
  	    	if(!checkLoginStatus()){return;}
  	    	var groupTree = $('#groupListPage .groupTree');
        	//得到选中的节点
        	var node = groupTree.tree("getSelected");
        	if(null==node){
        		$.messager.alert('系统提示',"请选中要删除的角色选项！",'info');
        		return;
        	}
  	        //验证是否登录--结束
  	    	$.messager.confirm('确认对话框', '此操作将永久删除该条数据,请确认是否继续操作？', function(flag){
      			if (flag){
      				$.post(basePath+"/group/delGroup.do",{uuid:node.id},
      		    	function(jsonData){
      					if(jsonData.code=="00"){
      						$.messager.alert('系统提示','删除成功！','success');
      						$('#groupListPage .groupTree').tree('reload');
      					}else{
      						$.messager.alert('系统提示',jsonData.msg,'error');
      					}
      		    	},"json");
      			}
      		});
  	    }
      </script>
      </div>
  </body>
</html>
